
import { getInvoiceApi, postInvoiceApi, getAllApi, edtInvoiceApi } from './api';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'invoice',
    state: {
        detail: {},
        invoiceBasic: {},
        list: [],
        state: 1,
        title: 3,
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },
        changeInvoice(state, { obj }){
            console.log(state, obj);
            return { ...state, ...obj };
        },
    },
    effects: {
        *getDetailEFF({ obj }, {select,put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(getInvoiceApi, obj);
            if (result.code == 200) {
                const invoice = yield select((state)=>state.invoice);
                invoice.state=result.ret.list[0].invState;
                invoice.title=result.ret.list[0].invTitle;
                yield put({ type: 'save', data: {detail: result.ret.list[0]} });
                Toast.hide();
                return result;
            } else {

            }
        },
        *getAllEFF({}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(getAllApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
                Toast.hide();
            } else {

            }
        },

        *modifyEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(postInvoiceApi, obj);
            if (result.code == 200) {
                Toast.success(result.msg, 1.5);
                yield put(routerRedux.goBack());
            } else if (result.code == 400) {
                Toast.fail(result.msg, 1.5);
            }
            return result;
        },


    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                console.log(pathname, query, "invoice");
                if (query.invId) dispatch({type: 'getDetailEFF', obj: {invId: query.invId}})
                else dispatch({type: 'getAllEFF', obj: {pageNo: 1}})
            });
        },
    },
}
}
